package com.spark.gaea.utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ValidateUtil {
	private static final int _CHAR_BEGIN = 65;
	private static final int _CHAR_END = 122;
	private static final int _DIGIT_BEGIN = 48;
	private static final int _DIGIT_END = 57;
	private static final char[] _EMAIL_ADDRESS_SPECIAL_CHAR;
	private static final String _VARIABLE_TERM_BEGIN = "[$";
	private static final String _VARIABLE_TERM_END = "$]";
	private static final String _XML_BEGIN = "<?xml";
	private static final String _XML_EMPTY = "<root />";
	private static Pattern _ipAddressPattern;
	private static Pattern _variableNamePattern;
	private static Pattern _emailer;

	public static boolean equals(final boolean boolean1, final boolean boolean2) {
		return boolean1 == boolean2;
	}

	public static boolean equals(final byte byte1, final byte byte2) {
		return byte1 == byte2;
	}

	public static boolean equals(final char char1, final char char2) {
		return char1 == char2;
	}

	public static boolean equals(final double double1, final double double2) {
		return Double.compare(double1, double2) == 0;
	}

	public static boolean equals(final float float1, final float float2) {
		return Float.compare(float1, float2) == 0;
	}

	public static boolean equals(final int int1, final int int2) {
		return int1 == int2;
	}

	public static boolean equals(final long long1, final long long2) {
		return long1 == long2;
	}

	public static boolean equals(final Object obj1, final Object obj2) {
		return (obj1 == null && obj2 == null) || (obj1 != null && obj2 != null && obj1.equals(obj2));
	}

	public static boolean equals(final short short1, final short short2) {
		return short1 == short2;
	}

	public static boolean isAddress(final String address) {
		if (isNull(address)) {
			return false;
		}
		final String[] tokens = address.split("@");
		if (tokens.length != 2) {
			return false;
		}
		for (final String token : tokens) {
			for (final char c : token.toCharArray()) {
				if (Character.isWhitespace(c)) {
					return false;
				}
			}
		}
		return true;
	}

	public static boolean isAscii(final char c) {
		return c >= ' ' && c <= '~';
	}

	public static boolean isChar(final char c) {
		return c >= 'A' && c <= 'z';
	}

	public static boolean isChar(final String s) {
		if (isNull(s)) {
			return false;
		}
		for (final char c : s.toCharArray()) {
			if (!isChar(c)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isDate(final int month, final int day, final int year) {
		return isGregorianDate(month, day, year);
	}

	public static boolean isDigit(final char c) {
		return c >= '0' && c <= '9';
	}

	public static boolean isDigit(final String s) {
		if (isNull(s)) {
			return false;
		}
		for (final char c : s.toCharArray()) {
			if (!isDigit(c)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isDomain(final String domainName) {
		if (isNull(domainName)) {
			return false;
		}
		if (domainName.length() > 255) {
			return false;
		}
		final String[] arr$;
		final String[] domainNameArray = arr$ = domainName.split(".");
		for (final String domainNamePart : arr$) {
			final char[] domainNamePartCharArray = domainNamePart.toCharArray();
			for (int i = 0; i < domainNamePartCharArray.length; ++i) {
				final char c = domainNamePartCharArray[i];
				if (i == 0 && c == '-') {
					return false;
				}
				if (i == domainNamePartCharArray.length - 1 && c == '-') {
					return false;
				}
				if (!isChar(c) && !isDigit(c) && c != '-') {
					return false;
				}
			}
		}
		return true;
	}

	public static boolean isEmailAddress(final String emailAddress) {
		return !isNull(emailAddress) && ValidateUtil._emailer.matcher(emailAddress).matches();
	}

	public static boolean isEmailAddressSpecialChar(final char c) {
		for (int i = 0; i < ValidateUtil._EMAIL_ADDRESS_SPECIAL_CHAR.length; ++i) {
			if (c == ValidateUtil._EMAIL_ADDRESS_SPECIAL_CHAR[i]) {
				return true;
			}
		}
		return false;
	}

	public static boolean isGregorianDate(final int month, final int day, final int year) {
		if (month < 0 || month > 11) {
			return false;
		}
		final int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		if (month == 1) {
			int febMax = 28;
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
				febMax = 29;
			}
			return day >= 1 && day <= febMax;
		} else
			return day >= 1 && day <= months[month];
	}

	public static boolean isHex(final String s) {
		return !isNull(s);
	}

	public static boolean isHTML(final String s) {
		return !isNull(s) && (s.contains("<html>") || s.contains("<HTML>")) && (s.contains("</html>") || s
				.contains("</HTML>"));
	}

	public static boolean isIPAddress(final String ipAddress) {
		final Matcher matcher = ValidateUtil._ipAddressPattern.matcher(ipAddress);
		return matcher.matches();
	}

	public static boolean isJulianDate(final int month, final int day, final int year) {
		if (month < 0 || month > 11) {
			return false;
		}
		final int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		if (month == 1) {
			int febMax = 28;
			if (year % 4 == 0) {
				febMax = 29;
			}
			return day >= 1 && day <= febMax;
		} else
			return day >= 1 && day <= months[month];
	}

	public static boolean isEnName(final String name) {
		if (isNull(name)) {
			return false;
		}
		for (final char c : name.trim().toCharArray()) {
			if ((!isChar(c) && !Character.isWhitespace(c)) || c == ',') {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotNull(final Long l) {
		return !isNull(l);
	}

	public static boolean isNotNull(final Object obj) {
		return !isNull(obj);
	}

	public static boolean isNotNull(final Object[] array) {
		return !isNull(array);
	}

	public static boolean isNotNull(final String s) {
		return !isNull(s);
	}

	public static boolean isNull(final Long l) {
		return l == null || l == 0L;
	}

	public static boolean isNotNull(final List<?> list) {
		return !isNull(list);
	}

	public static boolean isNull(final List<?> list) {
		return list == null || list.isEmpty();
	}

	public static boolean isNull(final Object obj) {
		if (obj instanceof Long) {
			return isNull((Long) obj);
		}
		if (obj instanceof String) {
			return isNull((String) obj);
		}
		return obj == null;
	}

	public static boolean isNull(final Object[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isNull(final String s) {
		if (s == null) {
			return true;
		}
		int counter = 0;
		for (int i = 0; i < s.length(); ++i) {
			final char c = s.charAt(i);
			if (c != ' ') {
				if (counter > 3) {
					return false;
				}
				if (counter == 0) {
					if (c != 'n') {
						return false;
					}
				} else if (counter == 1) {
					if (c != 'u') {
						return false;
					}
				} else if ((counter == 2 || counter == 3) && c != 'l') {
					return false;
				}
				++counter;
			}
		}
		return counter == 0 || counter == 4;
	}

	public static boolean isNumber(final String number) {
		if (isNull(number)) {
			return false;
		}
		for (final char c : number.toCharArray()) {
			if (!isDigit(c)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isPassword(final String password) {
		if (isNull(password)) {
			return false;
		}
		if (password.length() < 6) {
			return false;
		}
		for (final char c : password.toCharArray()) {
			if (!isChar(c) && !isDigit(c)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isUrl(final String url) {
		if (isNotNull(url)) {
			try {
				new URL(url);
				return true;
			} catch (MalformedURLException ex) {}
		}
		return false;
	}

	public static boolean isVariableName(final String variableName) {
		if (isNull(variableName)) {
			return false;
		}
		final Matcher matcher = ValidateUtil._variableNamePattern.matcher(variableName);
		return matcher.matches();
	}

	public static boolean isVariableTerm(final String s) {
		return s.startsWith("[$") && s.endsWith("$]");
	}

	public static boolean isWhitespace(final char c) {
		return c == '\0' || Character.isWhitespace(c);
	}

	public static boolean isXml(final String s) {
		return s.startsWith("<?xml") || s.startsWith("<root />");
	}

	static {
		_EMAIL_ADDRESS_SPECIAL_CHAR = new char[]{'.', '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
				'_', '`', '{', '|', '}', '~'};
		ValidateUtil._ipAddressPattern = Pattern.compile(
				"\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		ValidateUtil._variableNamePattern = Pattern.compile("[_a-zA-Z]+[_a-zA-Z0-9]*");
		ValidateUtil._emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	}
}
